import Packet from "../net/Packet"
import Role from './Role'

// 账号角色列表返回
export default class AccountRolesOk {
	/**
	 * @param roles 角色列表
	 */
	public constructor(public roles: Role[]) { }

	public encode(): Packet {
		const packet = this._encode()
		packet.Encode(2040)
		return packet
	}

	public encodeMsg(): ByteBuffer {
		return this._encode().GetBuffer()
	}

	public static decode(packet: Packet): AccountRolesOk {
		const roles = []
		const rolesCount = packet.ReadUshort()
		for (let i = 0; i < rolesCount; i++) {
			roles.push(Role.decode(packet))
		}
		return new AccountRolesOk(roles)
	}

	private _encode(): Packet {
		const packet = new Packet()
		const rolesCount = this.roles.length
		packet.WriteUshort(rolesCount)
		for (let i = 0; i < rolesCount; i++) {
			const xxx = this.roles[i]
			packet.WriteBuffer(xxx.encodeMsg())
		}
		return packet
	}
}
